﻿using Microsoft.AspNetCore.Http;
using Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using Microsoft.EntityFrameworkCore;
using System.Data.SqlTypes;
using UtilsORM;
using UtilsORM.MySqlData;
using UtilsCache;
////using System.Web.Script.Serialization;

namespace GatewayServerService.Data
{
    public class DBIotDeviceWarn
    {

        //新增预警
        //public void addWarn(iot_device_linkage linkage, List<iot_device_linkage_condition> conditions)
        //{
        //    try
        //    {

        //        string iswarn = UtilMemoryCacheHelper.get<string>("iswarn_" + linkage.l_guid);
        //        if (!string.IsNullOrEmpty(iswarn) && iswarn == "false")
        //        {
        //            //没有开启预警
        //            return;
        //        }



        //        EFIotGatewayContext ef = new EFIotGatewayContext();
        //        iot_device_linkage_warn warn = ef.iot_device_linkage_warn.Where(c => c.w_status == 1 && c.w_l_guid == linkage.l_guid).FirstOrDefault();
        //        if (warn == null)
        //        {
        //            UtilMemoryCacheHelper.set<string>("iswarn_" + linkage.l_guid, "false");
        //            return;
        //        }



        //        //w_status0默认关闭，1预警，2取消预警
        //        //warn.w_warn_status 0无预警，1预警中
        //        if (warn.w_status == 1 && warn.w_warn_status == 1)
        //        {
        //            //当前已经预警中，无需重复
        //            return;
        //        }




        //        updateWarn(linkage.l_guid, linkage.l_title, 1);
        //        foreach (iot_device_linkage_condition item in conditions)
        //        {
        //            string dvalue = new DBIotDeviceValueCurrentCache().getDeviceValue(item.lc_d_guid, item.lc_key);
        //            iot_device_model_variable variable = new DBIotDeviceVariableCache().getVariableByKey(item.lc_key);
        //            new DBIotDeviceWarnLog().addWarnLog(linkage, 1, item, variable.v_name, dvalue);

        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.ToString());
        //    }
        //}
        ////取消预警
        //public void cancelWarn(iot_device_linkage linkage, List<iot_device_linkage_condition> conditions)
        //{
        //    try
        //    {

        //        string iswarn = UtilMemoryCacheHelper.get<string>("iswarn_" + linkage.l_guid);
        //        if (!string.IsNullOrEmpty(iswarn) && iswarn == "false")
        //        {//没有开启预警
        //            return;
        //        }

        //        EFIotGatewayContext ef = new EFIotGatewayContext();
        //        iot_device_linkage_warn warn = ef.iot_device_linkage_warn.Where(c => c.w_status == 1 && c.w_l_guid == linkage.l_guid).FirstOrDefault();
        //        if (warn == null)
        //        {
        //            UtilMemoryCacheHelper.set<string>("iswarn_" + linkage.l_guid, "false");
        //            return;
        //        }

        //        if (warn.w_warn_status == 0)
        //        {//当前已经无预警中，无需重复
        //            return;
        //        }



        //        //EFIotGatewayContext ef = new EFIotGatewayContext();
        //        //iot_device_linkage_warn warn = ef.iot_device_linkage_warn.Where(c => c.w_status == 1 && c.w_l_guid == linkage.l_guid && c.w_warn_status == 1).FirstOrDefault();
        //        //if (warn == null)
        //        //{
        //        //    return;
        //        //}

        //        updateWarn(linkage.l_guid, linkage.l_title, 0);
        //        foreach (iot_device_linkage_condition item in conditions)
        //        {
        //            string dvalue = new DBIotDeviceValueCurrentCache().getDeviceValue(item.lc_d_guid, item.lc_key);

        //            iot_device_model_variable variable = new DBIotDeviceVariableCache().getVariableByKey(item.lc_key);
        //            new DBIotDeviceWarnLog().addWarnLog(linkage, 0, item, variable.v_name, dvalue);


        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.ToString());
        //    }
        //}


        //public void updateWarn(string lguid, string title, int warn)
        //{

        //    EFIotGatewayContext ef = new EFIotGatewayContext();
        //    ef.iot_device_linkage_warn.Where(c => c.w_l_guid == lguid).
        //        ExecuteUpdate(c => c.SetProperty(p => p.w_warn_status, warn)
        //        .SetProperty(p => p.w_remark, title)
        //        );
        //}
    }
}
